package com.boxing.filter;

import javax.servlet.*;
import javax.servlet.annotation.*;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebFilter(filterName = "AllFilter", urlPatterns = "/*")
public class AllFilter implements Filter {
    public void init(FilterConfig config) throws ServletException {
        System.out.println("AllFilter init");
    }

    public void destroy() {
        System.out.println("AllFilter destroy");
    }

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws ServletException, IOException {
        HttpServletResponse httpServletResponse = (HttpServletResponse) response;
        // 解除浏览器端的跨域同源限制
        httpServletResponse.setHeader("Access-Control-Allow-Origin" ,"*");
        System.out.println("AllFilter chain.doFilter 之前");
        chain.doFilter(request, response);
        response.getWriter().print("我是filter");
        System.out.println("AllFilter chain.doFilter 之后");
    }
}
